﻿using System;
using System.Collections.Generic;
using System.Text;
using HtmlAgilityPack;

namespace InternetSpiderLib.Scenes.Actions
{
    public class HtmlUrlDataExtraction : HtmlBaseDataExtractionAction
    {
        /// <summary>
        /// 处理所有的Url
        /// </summary>
        /// <param name="data"></param>
        public override void Extraction(SceneContext data)
        {
            HtmlDocument document = data?.Context?.Get<HtmlDocument>();
            if (document == null)
                return;

            var url = data?.Context.Get<string>(SceneContextKey.Url);
            if (string.IsNullOrEmpty(url))
                return;

            var baseUri = new Uri(url);

            var listA = document.DocumentNode.SelectNodes("//a");
            if (listA != null)
            {
                foreach (HtmlNode item in listA)
                {
                    var urlLink = item.Attributes["href"].Value;
                    if (string.IsNullOrEmpty(urlLink))
                        continue;

                    data.Uris.Add(new Uri(baseUri, urlLink));
                }
            }
        }
    }
}
